package cuttings;

import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.SwingUtilities;
import javax.swing.filechooser.*;
import java.net.URL;
import java.io.IOException;

/*
 * FileChooserDemo.java uses these files:
 *   images/Open16.gif
 *   images/Save16.gif
 */


 public class FileChooserDemo extends JPanel
  {
    static private final String newline = "\n";
    JButton openButton, saveButton;
    JTextArea log;
    JFileChooser fc;
    JLabel filename;
    static String name_file;

    public FileChooserDemo() {
        super(new BorderLayout());
        
        /*SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                //Turn off metal's use of bold fonts
                UIManager.put("swing.boldMetal", Boolean.FALSE); 
                createAndShowGUI();
            }
        });*/
        //Turn off metal's use of bold fonts
       

        //Create the log first, because the action listeners
        //need to refer to it.
        log = new JTextArea(5,20);
        log.setMargin(new Insets(5,5,5,5));
        log.setEditable(false);
        JScrollPane logScrollPane = new JScrollPane(log);
    }
    
    protected static ImageIcon createImageIcon(String path) {
        java.net.URL imgURL = FileChooserDemo.class.getResource(path);
        if (imgURL != null) {
            return new ImageIcon(imgURL);
        } else {
            System.err.println("Couldn't find file: " + path);
            return null;
        }
    }

    public String file_upload()
    {
      //Create a file chooser
        fc = new JFileChooser();
        
        int returnVal = fc.showOpenDialog(FileChooserDemo.this);

            if (returnVal == JFileChooser.APPROVE_OPTION) {
                File file = fc.getSelectedFile();
                name_file = fc.getName();
                
                //This is where a real application would open the file.
                log.append("Opening: " + file.getName() + "." + newline);
                
                //return file.getName();
                return file.getAbsolutePath();
              
            } else {
                log.append("Open command cancelled by user." + newline);
                
                return "Invalid";
            }
    }
    /**
     * Create the GUI and show it.  For thread safety,
     * this method should be invoked from the
     * event dispatch thread.
     */
    public String createAndShowGUI() 
    {
       //String file_name;
        //Create and set up the window.
        //JFrame frame = new JFrame("FileChooserDemo");
        //frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Add content to the window.
        Menu.base1.add(new FileChooserDemo());
        return file_upload();
//Display the window.
        //frame.pack();
        //frame.setVisible(true);
    }
    
    public static String returnFilename() 
    {
      return name_file;
    }

}
 